<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="上传方">
        <el-select v-model="searchForm.user_type" placeholder="请选择">
          <el-option v-for="item in user_type_list" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序">
        <el-select v-model="listQuery.sort" placeholder="排序" clearable >
          <el-option label="默认" value=""></el-option>
          <el-option label="时间倒叙" value="id desc"></el-option>
          <el-option label="时间正序" value="id asc"></el-option>
          <el-option label="从大到小" value="size desc"></el-option>
          <el-option label="从小到大" value="size asc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95">
        <template v-slot:default="{scope,row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="类型" width="110">
        <template v-slot:default="{scope,row}">
          {{ row.file_type||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="来源" width="120">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.user_type==1" type="success">后台</el-tag>
          <el-tag v-if="row.user_type==2" type="primary">用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="大小" width="100">
        <template v-slot:default="{scope,row}">
          {{ row.size||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="上传人" width="130">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.user_id>0"   type="primary" @click="$router.push({ path: '/user/index',query:{user_id:row.user_id} })">{{row.user_account||row.user_id}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="名称" width="200">
        <template v-slot:default="{scope,row}">
          {{ row.name||'/' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="储存方式" width="100" prop="upload_type_desc" />

<!--      <el-table-column align="center" label="路径" width="200">
        <template v-slot:default="{scope,row}">
          {{ row.path }}
        </template>
      </el-table-column>-->

      <el-table-column align="center" label="预览图" width="100">
        <template v-slot:default="{scope,row}">
          <el-image v-if="row.preview_url" :src="row.preview_url" :preview-src-list="[row.preview_url]" style="width: 40px; height: 40px;"/>
        </template>
      </el-table-column>

      <el-table-column align="left" label="MD5" width="180">
        <template v-slot:default="{scope,row}">
          {{ row.md5||'/' }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>


      <el-table-column align="center" prop="created_at" label="操作" width="150" fixed="right">
        <template v-slot:default="{scope,row}">
          <el-row>
            <el-button size="mini" type="primary" icon="el-icon-view" circle
                       @click="$router.push({path:'upload_log_form',query:{id:row.id}})"
            ></el-button>
            <el-button size="mini" type="success"   circle
                       @click="copyText(row.full_url)"
            ><i class="el-icon-copy-document"/></el-button>
            <el-button v-if="row.enable_del" size="mini" type="info" icon="el-icon-delete" circle @click="del(row.id)"></el-button>

          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/upload_log'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {

  },
  data() {
    return {
      user_type_list: [
        {
          value: 1,
          label: '后台'
        },
        {
          value: 2,
          label: '用户'
        }
      ],
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        all: 1,
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {

        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ id: id }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }

}
</script>
